Skip to content

fix(kt-client): a ping that cannot be sent leaves the drop to the socket's close, so a policy close is never retried (#973) - #1009

Merged
davideast merged 1 commit into
mainfrom
fix/kt-heartbeat-failed-ping-not-a-drop
Oct 16, 2026
Merged

davideast merged 1 commit into
mainfrom
fix/kt-heartbeat-failed-ping-not-a-drop

Conversation

@davideast

@davideast davideast commented Oct 15, 2026 •

Copy link
Copy Markdown
Owner

The Kotlin bridge client no longer reconnects after a policy close (1008) that happens while a heartbeat ping is being sent. This fixes a race from #993 that failed the Kotlin job on #1008's CI.

import dev.pyric.bridge.BridgeConnectionState
import dev.pyric.bridge.PyricBridgeClient

val bridge = PyricBridgeClient(url = "ws://127.0.0.1:5174/__pyric/sandbox")
bridge.connect()

// The host closes the socket with 1008 just as the client sends a heartbeat ping.
// Before: OkHttp refused the ping, the heartbeat reported a drop with no close code,
//         and the client went INTERRUPTED and reconnected. The real 1008 then arrived
//         for the old socket and was ignored.
// Now:    the refused ping ends the heartbeat quietly; the socket's own close reports 1008.
bridge.connectionStates.value // CLOSED, and no second attach

Only silence past 45 seconds is a drop the heartbeat reports

Heartbeat sees Before Now
No frame for the liveness timeout Drop: INTERRUPTED, reconnect Unchanged
The socket refuses the ping (send returns false: it is closing) Drop with no close code: INTERRUPTED, reconnect Heartbeat ends; the socket's close callback reports the drop with its real close code
The socket is no longer the current one Heartbeat ends Unchanged

This matches the Swift (try? await sendRaw) and Flutter (_sendPing: "a failed send means the socket is closing; its close is the drop") clients. ADR-0019's heartbeat amendment does not say a failed send is a drop, so it does not change.

Risk

  • A socket that refuses a frame but never reports its close would no longer be caught by the heartbeat. OkHttp's send returns false only after a close has started or the socket has failed, and both report through onClosing/onClosed/onFailure, so the drop is still reported.

Integration evidence

Unit evidence only: no CI job runs a native client against a real host. The new test forces the race's order on the fake socket (FakeReconnectSocket.beginClosing() makes send return false without reporting a close; finishClosing(1008) reports it later).

"Native SDK conformance (kotlin)" passed three times on ecd78fe, with the new test and all 34 BridgeReconnectTest cases passing: job 116641275127, and re-runs 116654616946 and 116655525208. The job is non-blocking, so CI / required does not reflect it.

Implementation notes
  • BridgeHeartbeat.beat treats a ping that returns false or throws as the end of the heartbeat, never as silence. onSilent runs only when the silence check passes the timeout. The start doc says so.
  • PyricBridgeClient.sendPing returns the transport's send result instead of throwing on false.
  • New test: a ping refused by a closing socket leaves the drop to its close, so a policy close is not retried. It waits for a refused ping, waits 100 ms, and asserts the client is still ATTACHED with one connect call. Then it delivers 1008 and asserts CLOSED, one attach and one connect call. On the previous code the refused ping threw, onSilent called handleConnectionLoss(closeCode = null), a retry was permitted, and the 5 ms reconnect delay produced a second connect call well within the 100 ms, so the connect-count assertion fails. If the client had re-attached, the later 1008 would have been delivered to a stale generation, so CLOSED would also never come.
  • Existing heartbeat tests still hold: the silent-socket and supplied-transport tests use sockets whose send returns true, so they still reach the timeout and onSilent. In no ping reaches a socket after it drops or after disconnect, a ping that races the drop is now refused quietly instead of reporting a stale drop.

Refs #973

…ket's close, so a policy close is never retried (#973)

When the host closed a socket with 1008, a heartbeat ping could reach
the socket while it was closing. OkHttp's send returned false, sendPing
threw, and the heartbeat reported a drop with no close code, which
permits a retry: the client went INTERRUPTED and reconnected, and the
real 1008 close then arrived for a stale generation and was ignored.
This failed "a policy close (1008) closes the client instead of
reconnecting" on #1008's CI.

A ping the socket refuses now ends the heartbeat quietly. The socket's
close callback reports the drop with its real code. Only silence past
the liveness timeout reports a drop from the heartbeat, as in the Swift
and Flutter clients.

The new test holds the socket in a closing state, waits for a refused
ping, and only then delivers the 1008 close. Before this change the
refused ping started a reconnect, so the test's connect-count check
fails.

Refs #973
@davideast
davideast merged commit a57efc0 into main Oct 16, 2026
60 checks passed
@davideast
davideast deleted the fix/kt-heartbeat-failed-ping-not-a-drop branch October 16, 2026 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant